<template>
    <div class="layout-route">
        <div class="card card-search">
            <el-form :model="queryParams" ref="queryForm" v-show="showSearch" inline label-width="100px">
                <el-form-item label="订单ID" prop="order_id">
                    <el-input type="textarea" :rows="4" v-model.trim="queryParams.order_id" placeholder="订单ID" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="订单编号" prop="order_number">
                    <el-input type="textarea" :rows="4" v-model.trim="queryParams.order_number" placeholder="订单编号" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="店铺" prop="shop_id">
                    <el-select v-model="queryParams.shop_id" placeholder="请选择店铺" clearable size="small" style="width: 240px">
                        <el-option v-for="(dict, index) in shopList" :key="index" :label="dict.shop_name" :value="dict.shop_id" />
                    </el-select>
                </el-form-item>
                <el-form-item label="sku" prop="sku">
                    <el-input v-model.trim="queryParams.sku" placeholder="sku" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="包裹ID" prop="package_id">
                    <el-input v-model.trim="queryParams.package_id" placeholder="包裹ID" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="物流单号" prop="tracking_number">
                    <el-input v-model.trim="queryParams.tracking_number" placeholder="物流单号" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
                </el-form-item>

                <!-- <el-form-item label="包裹状态" prop="payout_status">
                    <el-select v-model="queryParams.payout_status" placeholder="请选择店铺" clearable size="small" style="width: 240px">
                        <el-option v-for="(dict, index) in trackList" :key="index" :label="dict.label" :value="dict.value" />
                    </el-select>
                </el-form-item> -->

                <el-form-item label="创建时间" prop="create_time">
                    <el-date-picker
                        v-model="queryParams.create_time"
                        format="yyyy-MM-dd"
                        type="daterange"
                        start-placeholder="开始时间"
                        end-placeholder="结束时间"
                        style="width: 240px"
                    ></el-date-picker>
                </el-form-item>

                <el-form-item label=" ">
                    <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
        </div>

        <div class="card">
            <!-- <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button type="warning" plain icon="el-icon-download" :loading="download" :disabled="download" @click="handleExport">导出</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row> -->
            <div class="table-wrap">
                <el-table v-loading="loading" :data="tableData" :span-method="arraySpanMethod" default-expand-all script class="expend-table">
                    <el-table-column label="" type="expand" width="50">
                        <template slot-scope="scope">
                            <el-table :data="scope.row.sku_info" :span-method="objectSpanMethod" border :show-header="false" class="sub-table">
                                <el-table-column label="商品信息" prop="order_id" min-width="250">
                                    <template slot-scope="subScope">
                                        <div class="flex">
                                            <div style="width: 60px">
                                                <ImageUploadImg v-model="subScope.row.imange_url" :readOnly="true" class="small-img"></ImageUploadImg>
                                            </div>
                                            <div>
                                                <div class="flex">
                                                    <el-link type="primary">{{ subScope.row.sku }}</el-link> 
                                                    <span style="margin:0 4px 0 8px;">x</span> 
                                                    <div class="quantity-tag" :class="subScope.row.quantity > 1 ? 'red' : ''">{{ subScope.row.quantity }}</div>
                                                </div>
                                                <div>USD {{ subScope.row.price }}</div>
                                                <div>Variants: {{ subScope.row.variant_title }}</div>
                                            </div>
                                        </div>
                                    </template>
                                </el-table-column>
                                <el-table-column label="订单金额" min-width="100">
                                    <template slot-scope="subScope">
                                        <span>{{ scope.row.currency }} {{ scope.row.total_price }}</span>
                                    </template>
                                </el-table-column>
                                <el-table-column label="收件人[国家/地区]" width="180">
                                    <template slot-scope="subScope">
                                        <span>{{ scope.row.customer_name }}[{{ scope.row.country }}]</span>
                                    </template>
                                </el-table-column>
                                <el-table-column label="订单号" min-width="240">
                                    <template slot-scope="subScope">
                                        <el-link type="primary">平台订单号：{{ scope.row.order_number }}</el-link>
                                        <div>订单ID：{{ scope.row.order_id }}</div>
                                    </template>
                                </el-table-column>
                                <el-table-column label="时间" prop="create_time" min-width="200">
                                    <template slot-scope="subScope">
                                        <span>下单：{{ scope.row.create_time }}</span>
                                        <span v-if="scope.row.send_time">发货：{{ scope.row.send_time }}]</span>
                                        <span v-if="scope.row.little_time">缺货：{{ scope.row.little_time }}]</span>
                                    </template> </el-table-column
                                ><el-table-column label="物流方式" prop="tracking_name" min-width="180">
                                    <template slot-scope="subScope">
                                        <div v-if="!scope.row.tracking_name">物流方式未选择</div>
                                        <el-button type="text" icon="el-icon-edit-outline">{{ scope.row.tracking_name || '请选择物流方式' }}</el-button>
                                    </template>
                                </el-table-column>
                                <el-table-column label="物流状态" prop="order_status" min-width="180">
                                    <template slot-scope="subScope">
                                        {{ scope.row.order_status | formatLabelByValueFuzzy(orderStatusList) }}
                                    </template>
                                </el-table-column>
                                <el-table-column label="操作" width="80" fixed="right">
                                    <template slot-scope="subScope">
                                        <el-button type="text" icon="el-icon-edit" @click="showDetailDialog(scope.row)">详情</el-button>
                                    </template>
                                </el-table-column>
                            </el-table>
                        </template>
                    </el-table-column>
                    <el-table-column label="商品信息" prop="package_id" min-width="200"></el-table-column>
                    <el-table-column label="订单金额" min-width="100"></el-table-column>
                    <el-table-column label="收件人[国家/地区]" width="180"></el-table-column>
                    <el-table-column label="订单号" min-width="240"></el-table-column>
                    <el-table-column label="时间" prop="code_title" min-width="200">
                        <template slot-scope="scope"> 买家指定：{{ scope.row.code_title }} </template>
                    </el-table-column>
                    <el-table-column label="物流方式" min-width="180">
                    </el-table-column>
                    <el-table-column label="状态" prop="track_status" min-width="180">
                        <template slot-scope="scope"> [{{ scope.row.shop_name }}] </template>
                    </el-table-column>
                    <el-table-column label="操作" width="80" fixed="right"></el-table-column>
                </el-table>
            </div>
            <pagination v-show="total > 0" :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="getList"></pagination>
        </div>

        <detail-dialog v-model="detailDialog.visible" v-if="detailDialog.visible" :params="detailDialog.data" :type="detailDialog.type" @success="handleQuery"></detail-dialog>
    </div>
</template>

<script>
import { STATUS_LIST } from './constant'
import { queryAllOrderList, getTrackList } from '@/api/order'
import { getShopList } from '@/api/shop'
import { exportFile } from '@/utils/index'
import { parseTime } from '@/utils/quickadmin'
import DetailDialog from './components/DetailDialog.vue'
export default {
    name: 'orderbalanceList',
    components: { DetailDialog },
    data() {
        return {
            STATUS_LIST,
            // 遮罩层
            loading: true,
            download: false,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            salesmanList: [],
            // 表格数据
            tableData: [],
            // 店铺列表下拉
            shopList: [],
            trackList: [],
            // 系统订单状态 1:待审核 2:待处理 3:运单号申请 4:待打单 5:已交运 6:其他
            orderStatusList: [
                { label: '待审核', value: 1 },
                { label: '待处理', value: 2 },
                { label: '运单号申请', value: 3 },
                { label: '待打单', value: 4 },
                { label: '已交运', value: 5 },
                { label: '其他', value: 6 },
            ],
            // platformList: [
            //     {
            //         id: 'InStock',
            //         title: '有货'
            //     },
            //     {
            //         id: 'OutOfStock',
            //         title: '缺货'
            //     }
            // ],
            payment_gateway_names: [
                {
                    id: 'NotSentWithin24Hours',
                    title: '24小时未发',
                },
                {
                    id: 'NotSentWithin48Hours',
                    title: '48小时未发',
                },
                {
                    id: 'NotSentFor7Days',
                    title: '7天未发',
                },
                {
                    id: 'More',
                    title: '更多',
                },
            ],
            pageInfo: {
                page: 1,
                limit: 10,
            },
            spends_Total: null,
            spends: [],
            // 查询参数
            queryParams: {
                order_id: undefined,
                order_number: undefined,
                sku: undefined,
                shop_id: undefined,
                package_id: undefined,
                tracking_number: undefined,
                create_time: [],
            },
            detailDialog: {
                visible: false,
                data: {},
            },
        }
    },
    async created() {
        this.loading = true
        try {
            const shopData= await getShopList()
            this.shopList = shopData?.data?.list ?? []
            // const [shopData, TrackData] = await Promise.all([getShopList(), getTrackList()])
            // this.shopList = shopData?.data?.list ?? []
            // this.trackList = TrackData?.data ?? []
        } catch (error) {
            this.loading = false
            console.log(error)
        }
        this.getList()
    },
    methods: {
        arraySpanMethod({ row, column, rowIndex, columnIndex }) {
            if (columnIndex === 5) {
                return [1, 2]
            } else if (columnIndex === 6) {
                return [0, 0]
            } else if (columnIndex === 7) {
                return [1, 2]
            } else if (columnIndex === 8) {
                return [0, 0]
            }
        },
        objectSpanMethod({ row, column, rowIndex, columnIndex }) {
            const skuList = this.tableData.find(item => item.order_id === row.order_id)?.sku_info
            // 判断是否需要合并单元格的列
            if ([1,2,3,4,5,6,7].includes(columnIndex)) {
                if(rowIndex === 0) {
                    return {
                        rowspan: skuList.length,
                        colspan: 1
                    };
                } else {
                    return {
                        rowspan: 0,
                        colspan: 0
                    };
                }
               
            }
        },
        showDetailDialog(row) {
            this.detailDialog.visible = true
            this.detailDialog.data = row
            this.detailDialog.type = row.advertiser_id ? 'edit' : 'add'
        },
        toAdvcampaign(row) {
            this.$router.push({
                path: '/orderbalance/cost/advcampaignCost',
                query: {
                    advertiser_id: row.advertiser_id,
                },
            })
        },
        handleExport() {
            this.download = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            if (qyparams.createtime?.length == 2) {
                qyparams.createtime = [parseTime(qyparams.createtime[0], 'y-m-d'), parseTime(qyparams.createtime[1], 'y-m-d')]
            }
            if (qyparams.processed_at?.length == 2) {
                qyparams.processed_at = [parseTime(qyparams.processed_at[0], 'y-m-d'), parseTime(qyparams.processed_at[1], 'y-m-d')]
            }
            exportordertrack(qyparams)
                .then(res => {
                    exportFile(res, () => {
                        this.download = false
                    })
                })
                .catch(err => {
                    this.download = false
                })
        },
        //列表
        getList() {
            this.loading = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            if (qyparams.createtime?.length == 2) {
                qyparams.createtime = [parseTime(qyparams.createtime[0], 'y-m-d'), parseTime(qyparams.createtime[1], 'y-m-d')]
            }
            if (qyparams.processed_at?.length == 2) {
                qyparams.processed_at = [parseTime(qyparams.processed_at[0], 'y-m-d'), parseTime(qyparams.processed_at[1], 'y-m-d')]
            }
            queryAllOrderList(qyparams)
                .then(response => {
                    this.tableData = response.data.list
                    this.total = response.data.total
                })
                .finally(() => {
                    this.loading = false
                })
        },
        orderbalanceConfirm(row) {
            this.$confirm('是否确认校对', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                orderbalanceConfirm({ id: row.id }).then(() => {
                    this.$message.success('操作成功')
                    this.handleQuery()
                })
            })
        },

        // 搜索按钮操作
        handleQuery() {
            this.pageInfo.page = 1
            this.getList()
        },

        // 重置按钮操作
        resetQuery() {
            this.resetForm('queryForm')

            this.handleQuery()
        },
    },
}
</script>
<style scoped>

::v-deep .expend-table .el-table__expanded-cell[class*='cell'] {
    padding: 0 !important;
}
::v-deep .el-tabs .el-tabs__content {
    padding: 0;
    border-bottom: 0;
}
.el-tabs--border-card {
    box-shadow: none;
    border-bottom: 0;
}

::v-deep .el-tabs .el-tabs__content {
    padding: 0;
    border-bottom: 0;
}
.table-wrap {
    /* padding: 15px; */
    border-bottom: 1px solid #dfe4ed;
    border-left: 1px solid #dfe4ed;
    border-right: 1px solid #dfe4ed;
    box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.12), 0 0 6px 0 rgba(0, 0, 0, 0.04);
}
.total-warp {
    font-size: 18px;
    font-weight: bold;
    padding: 8px 0 16px 0;
}
.quantity-tag {
    min-width: 16px;
    height: 16px;
    line-height: 16px;
    border-radius: 16px;
    font-size: 10px;
    text-align: center;
    background: #ddd;
}
.quantity-tag.red {
        background: red;
        color: #fff;
    }
</style>
<style lang="scss" scoped>
::v-deep .sub-table {
    border: 0;
    &::after{
        background: transparent;
    }
    /* 隐藏子表格的滚动条 */
    .el-table__body-wrapper {
        overflow-x: hidden !important;
    }
}

::v-deep .small-img {
    .upload-img-wrap {
        width: 54px;
    }
    .upload-img-box {
        width: 50px;
        height: 50px;
    }
    .upload-img {
        width: 48px;
        height: 48px;
    }
}
</style>
